﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;


public partial class NumTextbox : TextBox
{
    private int _num;
    public NumTextbox()
    {
        InitializeComponent();
    }
    private const int WM_CONTEXTMENU = 0x007b;//右键菜单消息 
    private const int WM_CHAR = 0x0102;       //输入字符消息（键盘输入的，输入法输入的好像不是这个消息）
    private const int WM_CUT = 0x0300;        //程序发送此消息给一个编辑框或combobox来删除当前选择的文本
    private const int WM_COPY = 0x0301;       //程序发送此消息给一个编辑框或combobox来复制当前选择的文本到剪贴板
    private const int WM_PASTE = 0x0302;      //程序发送此消息给editcontrol或combobox从剪贴板中得到数据
    private const int WM_CLEAR = 0x0303;      //程序发送此消息给editcontrol或combobox清除当前选择的内容；
    private const int WM_UNDO = 0x0304;        //程序发送此消息给editcontrol或combobox撤消最后一次操作

    #region 属性

    public int Num {
        get
        {    //if (this.Text != "")
        //        return Convert.ToInt32(this.Text.Replace(" ", ""));
        //    else
                return _num;
        }
        //set
        //{
        //    _num = value;
        //    base.Text = _num.ToString();
        //}
    }

    #endregion

    /// <summary>
    /// 重写 WndProc
    /// </summary>
    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_CHAR:
                int i = (int)m.WParam;
                bool isBack = (i == (int)Keys.Back);
                bool isDelete = (i == (int)Keys.Delete);//实际上这是一个"."键
                bool isCtr = (i == 24) || (i == 22) || (i == 26) || (i == 3);

                if (isBack || isDelete || isCtr)
                {
                    //控制键不作处理
                }
                else
                {
                    char c = (char)i;
                }
                base.WndProc(ref m);
                break;
            case WM_PASTE:
                IDataObject iData = Clipboard.GetDataObject();//取剪贴板对象
                if (iData.GetDataPresent(DataFormats.Text)) //判断是否是Text
                {
                    string text = (string)iData.GetData(DataFormats.Text);//取数据
                }
                base.WndProc(ref m);
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);
        //
        if ((e.KeyChar >= '0' && e.KeyChar <= '9')
            || e.KeyChar == 32 || e.KeyChar == 8 || e.KeyChar == 16)
        {
            // 转成大写
            e.KeyChar = char.ToUpper(e.KeyChar);
            e.Handled = false;
        }
        else
        {
            MessageBox.Show("请输入数字【0-9】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //MessageBox.Show("请输入16进制字符");
            e.Handled = true;//阻拦字符键入
        }
        
    }

    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        if (this.Text != "")
            _num = Convert.ToInt32(this.Text);
        else
            _num = 0;
}
}
